package com.changgou.web.gateway.filter;

import com.changgou.web.gateway.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Author LQQ
 * @Date 2020/3/10 19:16
 * @Version 1.0
 */

/**
 * 客户端网管全局过滤器
 */
@Component
public class AuthFilter implements GlobalFilter, Ordered {
  //定义一个跳转页面的常量
    private static final String LOGIN_URL = "http://localhost:8001/api/oauth/toLogin";



    @Autowired
    private AuthService authService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1. 获取请求对象
        ServerHttpRequest request = exchange.getRequest();

        //2. 获取响应对象
        ServerHttpResponse response = exchange.getResponse();

        //3. 判断当前请求路径是否为登入请求,如果是则直接放行
        //3.1 获取登入请求的路径
             String path = request.getURI().getPath();

        if ("/api/oauth/login".equals(path) || ! UrlFilter.hasAuthorize(path) ){
            //直接放行
            return chain.filter(exchange);
        }

        //4. 从cookie中获取jti的值,如果该值不存在拒绝本次访问
        String jti = authService.GetJtiFromCookie(request);

        if (StringUtils.isEmpty(jti)) {
            //拒绝访问
          //  response.setStatusCode(HttpStatus.UNAUTHORIZED);
          //  return response.setComplete();

            //没有登入时访问购物车,直接跳转登入页面
            return this.toLoginPage(LOGIN_URL+"?FROM="+request.getURI().getPath(),exchange);


        }

        //5. 从Redis中获取jwt的值,如果不存在,拒绝访问
        String jwt = authService.getJwtFromRedis(jti);

        if (StringUtils.isEmpty(jwt)) {
            //拒绝访问
           // response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //return response.setComplete();

            //没有登入时访问购物车,直接跳转登入页面
            return this.toLoginPage(LOGIN_URL,exchange);



        }

        //6. 对当前的请求对象进行增强,让它携带令牌的信息
        request.mutate().header("Authorization", "Bearer " + jwt);


        return chain.filter(exchange);
    }


    /**
     * 跳转登入页面
     * @param loginUrl
     * @param exchange
     * @return
     */
    private Mono<Void> toLoginPage(String loginUrl, ServerWebExchange exchange) {

        ServerHttpResponse response = exchange.getResponse();

        response.setStatusCode(HttpStatus.SEE_OTHER);

        //Location 不能更改,进行页面跳转
        response.getHeaders().set("Location",loginUrl);

        return response.setComplete();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
